package com.liu.blindBox.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.liu.blindBox.entity.qo.SaleInfoQO;
import com.liu.blindBox.entity.qo.ShoppingCartInfoQO;
import com.liu.blindBox.entity.vo.ShoppingCartBlindBoxInfoVO;
import com.liu.blindBox.service.ShoppingCartService;
import com.liu.common.entity.Result;
import com.liu.common.entity.StatusCode;
import com.liu.common.service.GetTokenService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @Resource
    private GetTokenService getTokenService;

    /**
     * 添加购物车
     * @param shoppingCartInfoQO 添加的盲盒id和数量
     * @return Result
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/addShoppingCart")
    public Result addShoppingCart(@RequestBody ShoppingCartInfoQO shoppingCartInfoQO){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            shoppingCartService.addBlindBoxShoppingCart(String.valueOf(loginIdByToken), shoppingCartInfoQO);
            return new Result(true, StatusCode.OK,"添加成功");
        }else {
            return new Result(false, StatusCode.ERROR,"添加失败,您还未登录");
        }
    }

    /**
     * 删除购物车
     * @param shoppingCartInfoQO
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/deleteShoppingCart")
    public Result deleteShoppingCart(@RequestBody ShoppingCartInfoQO shoppingCartInfoQO){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            shoppingCartService.deleteBlindBoxShoppingCart(String.valueOf(loginIdByToken), shoppingCartInfoQO);
            return new Result(true, StatusCode.OK,"删除成功");
        }else {
            return new Result(false, StatusCode.ERROR,"您还未登录,添加失败");
        }
    }

    /**
     * 查询购物车
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/queryShoppingCart")
    public Result queryShoppingCart(){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            List<ShoppingCartBlindBoxInfoVO> shoppingCartBlindBoxInfoVOS = shoppingCartService.queryBlindBoxShoppingCart(String.valueOf(loginIdByToken));
            if (shoppingCartBlindBoxInfoVOS != null){
                return new Result(true, StatusCode.OK,"查询成功",shoppingCartBlindBoxInfoVOS);
            }
            return new Result(false, StatusCode.ERROR,"您的购物车中没有盲盒，快去添加吧");
        }else {
            return new Result(false, StatusCode.ERROR,"您还未登录,查询失败");
        }
    }

    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/buyBlindBox")
    public Result buyBlindBox(@RequestBody List<SaleInfoQO> saleInfoQOList){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            int i = shoppingCartService.buyBlindBox(saleInfoQOList, String.valueOf(loginIdByToken));
            if (i == 0){
                return new Result(false, StatusCode.ERROR,"余额不足");
            }else {
                return new Result(true, StatusCode.OK,"购买成功");
            }
        }else {
            return new Result(false, StatusCode.ERROR,"请登录后再尝试购买");
        }
    }

}
